//
//  BaseViewController.m
//  StockpdfReading
//
//  Created by 于洪东 on 2017/6/17.
//  Copyright © 2017年 上海LOL信息技术服务有限公司. All rights reserved.
//

#import "BaseViewController.h"


static char *btnClickAction;

@interface BaseViewController ()

@end

@implementation BaseViewController


#pragma mark --- life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self p_setNavBar];
}


#pragma mark ---- public method

-(void)setLeftButtonWithTitle:(NSString *)title Image:(NSString *)image SelectedImage:(NSString *)selectedImage Action:(void (^)())btnClickBlock{
    self.navLeftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.navLeftBtn setBackgroundColor:[UIColor clearColor]];
    objc_setAssociatedObject(self.navLeftBtn, &btnClickAction, btnClickBlock, OBJC_ASSOCIATION_COPY);
    [self setCustomButton:self.navLeftBtn Title:title Image:image SelectedImage:selectedImage];
    [self.navLeftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.navLeftBtn];
}

-(void)setRightButtonWithTitle:(NSString *)title Image:(NSString *)image SelectedImage:(NSString *)selectedImage Action:(void (^)())btnClickBlock{
    self.navRightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    objc_setAssociatedObject(self.navRightBtn, &btnClickAction, btnClickBlock, OBJC_ASSOCIATION_COPY);
    [self setCustomButton:self.navRightBtn Title:title Image:image SelectedImage:selectedImage];
    [self.navRightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.navRightBtn];
}

-(void)setCustomButton:(UIButton *)button Title:(NSString *)title Image:(NSString *)image SelectedImage:(NSString *)selectedImage{
    if (image) {
        [button setImage:[[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    if (selectedImage) {
        [button setImage:[[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateHighlighted];
    }
    if (title) {
        button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitle:title forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    }
    [button sizeToFit];
    [button addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark ---- ActionBtnClick
- (void)actionBtnClick:(UIButton *)btn {
    void (^btnClickBlock) (void) = objc_getAssociatedObject(btn, &btnClickAction);
    btnClickBlock();
}

#pragma mark --- private
#pragma mark - private method
-(void)p_setNavBar{
    // 不要隐藏
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    // 设置属性
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]}];
    //统一设置返回按钮
    if (self.navigationController.childViewControllers.count > 1) {
        __weak typeof(self) weakSelf = self;
        [self setLeftButtonWithTitle:nil Image:@"titlebar_icon_goback" SelectedImage:@"" Action:^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }
}






@end
